package com.ifreshwing.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ifreshwing.activity.base.TakePicActivity;
import com.ifreshwing.bean.ParkListBean;
import com.ifreshwing.userpark.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lz on 2017/5/2.
 */

public class ParkingListAllAdapter extends BaseAdapter {
    List<ParkListBean> list;
    Context context;

    public ParkingListAllAdapter(List<ParkListBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return list.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        convertView = LayoutInflater.from(context).inflate(R.layout.adapter_parking_card, null);
        vh = new ParkingListAllAdapter.ViewHolder(convertView);
        int status = list.get(position).getStatus();
        switch (status) {
            case 1:
                vh.tvOccupy.setText("占用");
                vh.ivOccupy.setImageResource(R.mipmap.occupy_blue);
                vh.tvOccupy.setTextColor(Color.parseColor("#1694e9"));
                vh.parkNum.setText(list.get(position).getParking_num());
                vh.tvLlCarInfoCarId.setText(list.get(position).getOccupy_carNum());
                vh.tvLlCarInfoPhone.setText(list.get(position).getPhone_num());
                vh.tvLlCarInfoRemainingTime.setText(list.get(position).getRemaining_time());

                vh.llCarInfo.setVisibility(View.VISIBLE);
                vh.llCamera.setVisibility(View.VISIBLE);
                vh.llRelease.setVisibility(View.VISIBLE);
                vh.llCall.setVisibility(View.VISIBLE);
                vh.llOpen.setVisibility(View.GONE);
                vh.llTimeInfo.setVisibility(View.GONE);
                vh.llRegion.setVisibility(View.GONE);
                vh.llTemporary.setVisibility(View.GONE);
                break;

            case 2:
                vh.tvOccupy.setText("空闲");
                vh.ivOccupy.setImageResource(R.mipmap.occupy_gray);
                vh.tvOccupy.setTextColor(Color.parseColor("#BFBFBF"));

                vh.parkNum.setText(list.get(position).getParking_num());

                vh.llCarInfo.setVisibility(View.GONE);
                vh.llTimeInfo.setVisibility(View.GONE);
                vh.llRegion.setVisibility(View.GONE);
                vh.llTemporary.setVisibility(View.GONE);

                vh.llCall.setVisibility(View.GONE);
                vh.llRelease.setVisibility(View.GONE);
                vh.llCamera.setVisibility(View.GONE);
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) vh.llOpen.getLayoutParams();
                params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                break;

            case 3:
                vh.tvOccupy.setText("违规");
                vh.ivOccupy.setImageResource(R.mipmap.occupy_red);
                vh.tvOccupy.setTextColor(Color.parseColor("#FF302E"));

                vh.parkNum.setText(list.get(position).getParking_num());
                vh.tvLlCarInfoCarId.setText(list.get(position).getOccupy_carNum());
                vh.tvLlCarInfoPhone.setText(list.get(position).getPhone_num());
                vh.tvLlCarInfoRemainingTime.setText(list.get(position).getRemaining_time());
                vh.tvLlCarInfoRemainingTime.setTextColor(Color.parseColor("#FF302E"));

                vh.tvLlTimeInfoStartTime.setText(list.get(position).getStart_time());
                vh.tvLlTimeInfoEndTime.setText(list.get(position).getEnd_time());
                vh.tvLlTimeInfoArrears.setText(list.get(position).getArrears() + "元");
                vh.tvRegion.setText(list.get(position).getRegion());
                vh.ivImgLeft.setImageResource(R.mipmap.test);
                vh.ivImgRight.setImageResource(R.mipmap.test);
                vh.llOpen.setVisibility(View.GONE);
                vh.llCamera.setVisibility(View.GONE);
                vh.llTemporary.setVisibility(View.GONE);

                params = (RelativeLayout.LayoutParams) vh.llCall.getLayoutParams();
                params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

                break;

            case 4:
                vh.tvOccupy.setText("违规");
                vh.ivOccupy.setImageResource(R.mipmap.occupy_red);
                vh.tvOccupy.setTextColor(Color.parseColor("#FF302E"));

                vh.parkNum.setText(list.get(position).getParking_num());
                vh.tvLlCarInfoCarId.setText(list.get(position).getOccupy_carNum());
                vh.tvLlCarInfoPhone.setText(list.get(position).getPhone_num());
                vh.tvLlCarInfoRemainingTime.setText(list.get(position).getRemaining_time());
                vh.tvLlTimeInfoStartTime.setText(list.get(position).getStart_time());
                vh.tvLlTimeInfoEndTime.setText(list.get(position).getEnd_time());
                vh.tvLlTimeInfoArrears.setText(list.get(position).getArrears() + "元");
                vh.tvRegion.setText(list.get(position).getRegion());
                vh.ivImgLeft.setImageResource(R.mipmap.test);
                vh.ivImgRight.setImageResource(R.mipmap.test);
                vh.llOpen.setVisibility(View.GONE);
                vh.llCamera.setVisibility(View.GONE);
                vh.llTemporary.setVisibility(View.GONE);

                params = (RelativeLayout.LayoutParams) vh.llCall.getLayoutParams();
                params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                break;

            case 5:
                vh.tvOccupy.setText("占用");
                vh.ivOccupy.setImageResource(R.mipmap.occupy_red);
                vh.tvOccupy.setTextColor(Color.parseColor("#FF302E"));

                vh.parkNum.setText(list.get(position).getParking_num());

                vh.llCarInfo.setVisibility(View.GONE);
                vh.llTimeInfo.setVisibility(View.GONE);
                vh.llTemporary.setVisibility(View.GONE);
                vh.llRegion.setVisibility(View.VISIBLE);
                vh.llCall.setVisibility(View.GONE);
                vh.llOpen.setVisibility(View.GONE);
                vh.llCamera.setVisibility(View.GONE);

                params = (RelativeLayout.LayoutParams) vh.llRelease.getLayoutParams();
                params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                vh.tvRegion.setText(list.get(position).getRegion());
                vh.ivImgLeft.setVisibility(View.GONE);
                vh.ivImgRight.setVisibility(View.GONE);
                break;

            case 6:
                vh.tvOccupy.setText("违规");
                vh.ivOccupy.setImageResource(R.mipmap.occupy_red);
                vh.tvOccupy.setTextColor(Color.parseColor("#FF302E"));
                vh.llRegion.setVisibility(View.VISIBLE);
                vh.parkNum.setText(list.get(position).getParking_num());
                vh.tvLlCarInfoCarId.setText(list.get(position).getOccupy_carNum());
                vh.tvLlCarInfoPhone.setText(list.get(position).getPhone_num());
                vh.tvLlCarInfoRemainingTime.setText(list.get(position).getRemaining_time());
                vh.tvLlTimeInfoStartTime.setText(list.get(position).getStart_time());
                vh.tvLlTimeInfoEndTime.setText(list.get(position).getEnd_time());
                vh.tvLlTimeInfoArrears.setText(list.get(position).getArrears() + "元");
                vh.tvRegion.setText(list.get(position).getRegion());
                vh.ivImgLeft.setImageResource(R.mipmap.test);
                vh.ivImgRight.setImageResource(R.mipmap.test);
                vh.llOpen.setVisibility(View.GONE);
                vh.llCamera.setVisibility(View.GONE);
                vh.llTemporary.setVisibility(View.GONE);

                params = (RelativeLayout.LayoutParams) vh.llCall.getLayoutParams();
                params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                break;

            case 7:
                vh.tvOccupy.setText("占用");
                vh.ivOccupy.setImageResource(R.mipmap.occupy_org);
                vh.tvOccupy.setTextColor(Color.parseColor("#ff802f"));

                vh.parkNum.setText(list.get(position).getParking_num());
                vh.tvLlCarInfoCarId.setText(list.get(position).getOccupy_carNum());
                vh.tvLlCarInfoPhone.setText(list.get(position).getPhone_num());
                vh.tvLlCarInfoRemainingTime.setText(list.get(position).getRemaining_time());
                vh.tvLlCarInfoRemainingTime.setTextColor(Color.parseColor("#ff802f"));
                vh.llCarInfo.setVisibility(View.VISIBLE);
                vh.llCamera.setVisibility(View.VISIBLE);
                vh.llOpen.setVisibility(View.GONE);
                vh.llTimeInfo.setVisibility(View.GONE);
                vh.llRegion.setVisibility(View.GONE);
                vh.llTemporary.setVisibility(View.GONE);
                break;

            case 8:
                vh.tvOccupy.setText("占用");
                vh.ivOccupy.setImageResource(R.mipmap.occupy_blue);
                vh.tvOccupy.setTextColor(Color.parseColor("#1694e9"));

                vh.parkNum.setText(list.get(position).getParking_num());
                vh.tvLlCarInfoCarId.setText(list.get(position).getOccupy_carNum());
                vh.tvLlCarInfoPhone.setText(list.get(position).getPhone_num());
                vh.tvLlCarInfoRemainingTime.setText(list.get(position).getRemaining_time());
                vh.llTimeInfo.setVisibility(View.VISIBLE);
                vh.tvLlTimeInfoArrears.setVisibility(View.GONE);
                vh.tvLlTimeInfoEndTime.setVisibility(View.GONE);
                vh.tvLlTimeInfoStartTime.setText(list.get(position).getStart_time());
                vh.llRegion.setVisibility(View.GONE);
                vh.llOpen.setVisibility(View.GONE);
                vh.llTemporary.setVisibility(View.GONE);
                vh.ll_ll_timeInfo_arrears.setVisibility(View.GONE);
                vh.ll_ll_timeInfo_endtime.setVisibility(View.GONE);

                break;

            case 9:
                vh.tvOccupy.setText("占用");
                vh.ivOccupy.setImageResource(R.mipmap.occupy_blue);
                vh.tvOccupy.setTextColor(Color.parseColor("#1694e9"));

                vh.parkNum.setText(list.get(position).getParking_num());

                vh.tvLlCarInfoCarId.setText(list.get(position).getOccupy_carNum());
                vh.tvLlCarInfoPhone.setText(list.get(position).getPhone_num());
                vh.tvLlCarInfoRemainingTime.setText(list.get(position).getRemaining_time());
                vh.llTimeInfo.setVisibility(View.VISIBLE);
                vh.tvLlTimeInfoArrears.setVisibility(View.GONE);
                vh.tvLlTimeInfoEndTime.setVisibility(View.GONE);
                vh.tvLlTimeInfoStartTime.setText(list.get(position).getStart_time());
                vh.llRegion.setVisibility(View.GONE);
                vh.llOpen.setVisibility(View.GONE);
                vh.llTimeInfo.setVisibility(View.GONE);
                vh.llTemporary.setVisibility(View.VISIBLE);
                vh.tvLlTemporaryStartTime.setText(list.get(position).getTemporary_num());
                break;

            case 10:
                vh.tvOccupy.setText("占用");
                vh.ivOccupy.setImageResource(R.mipmap.occupy_blue);
                vh.tvOccupy.setTextColor(Color.parseColor("#1694e9"));

                vh.parkNum.setText(list.get(position).getParking_num());

                vh.tvLlCarInfoCarId.setText(list.get(position).getOccupy_carNum());
                vh.tvLlCarInfoPhone.setText(list.get(position).getPhone_num());
                vh.tvLlCarInfoRemainingTime.setText(list.get(position).getRemaining_time());
                vh.llTimeInfo.setVisibility(View.VISIBLE);
                vh.tvLlTimeInfoArrears.setVisibility(View.GONE);
                vh.tvLlTimeInfoEndTime.setVisibility(View.GONE);
                vh.tvLlTimeInfoStartTime.setText(list.get(position).getStart_time());
                vh.llRegion.setVisibility(View.GONE);
                vh.llOpen.setVisibility(View.GONE);
                vh.llTimeInfo.setVisibility(View.GONE);
                vh.llTemporary.setVisibility(View.GONE);
                vh.tvLlTemporaryStartTime.setText(list.get(position).getTemporary_num());
                break;

        }
        vh.llRelease.setOnClickListener(v -> {
            showDialog(position);
        });
        vh.llCamera.setOnClickListener(v -> {
            Intent it = new Intent(context, TakePicActivity.class);

        });


        return convertView;
    }


    private void showDialog(int pos) {
        final AlertDialog.Builder dialog = new AlertDialog.Builder(context);
        dialog.setTitle("提示").
                setMessage("确定要释放该车位吗").
                setPositiveButton("确定", (dialog1, which) -> {
                    list.get(pos).setStatus(2);
                    notifyDataSetChanged();
                }).setNegativeButton("取消", (dialog12, which) -> {
        }).show();
    }


    static class ViewHolder {
        @BindView(R.id.tv_occupy)
        TextView tvOccupy;
        @BindView(R.id.iv_occupy)
        ImageView ivOccupy;
        @BindView(R.id.parkNum)
        TextView parkNum;
        @BindView(R.id.tv_ll_carInfo_carId)
        TextView tvLlCarInfoCarId;
        @BindView(R.id.tv_ll_carInfo_phone)
        TextView tvLlCarInfoPhone;
        @BindView(R.id.tv_ll_carInfo_remaining_time)
        TextView tvLlCarInfoRemainingTime;
        @BindView(R.id.ll_carInfo)
        LinearLayout llCarInfo;
        @BindView(R.id.tv_ll_timeInfo_start_time)
        TextView tvLlTimeInfoStartTime;
        @BindView(R.id.ll_ll_timeInfo_endtime)
        LinearLayout ll_ll_timeInfo_endtime;
        @BindView(R.id.ll_ll_timeInfo_arrears)
        LinearLayout ll_ll_timeInfo_arrears;
        @BindView(R.id.tv_ll_timeInfo_end_time)
        TextView tvLlTimeInfoEndTime;
        @BindView(R.id.tv_ll_timeInfo_arrears)
        TextView tvLlTimeInfoArrears;
        @BindView(R.id.ll_timeInfo)
        LinearLayout llTimeInfo;
        @BindView(R.id.iv_img_left)
        ImageView ivImgLeft;
        @BindView(R.id.iv_img_right)
        ImageView ivImgRight;
        @BindView(R.id.tv_region)
        TextView tvRegion;
        @BindView(R.id.ll_region)
        LinearLayout llRegion;
        @BindView(R.id.textView)
        TextView textView;
        @BindView(R.id.tv_ll_temporary_start_time)
        TextView tvLlTemporaryStartTime;
        @BindView(R.id.tv_ll_temporary_temporary_num)
        TextView tvLlTemporaryTemporaryNum;
        @BindView(R.id.ll_temporary)
        LinearLayout llTemporary;
        @BindView(R.id.ll_camera)
        LinearLayout llCamera;
        @BindView(R.id.ll_call)
        LinearLayout llCall;
        @BindView(R.id.ll_release)
        LinearLayout llRelease;
        @BindView(R.id.ll_open)
        LinearLayout llOpen;
        @BindView(R.id.rl_menu)
        RelativeLayout rlMenu;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
